package org.esgl3d;

import java.util.Date;

import org.esgl3d.core.AndroidResourceResolver;
import org.esgl3d.primitives.Box;
import org.esgl3d.rendering.RenderActivity;
import org.esgl3d.rendering.Renderer;
import org.esgl3d.rendering.TextureManager;
import org.esgl3d.scene.MeshNode;
import org.esgl3d.scene.Scene;
import org.esgl3d.ui.BitmapFont;
import org.esgl3d.ui.Label;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory.Options;

public class ExBitmapFontActivity extends RenderActivity {
	private Label myLabel = null;
	private Renderer cur;
	private MeshNode box = null;
	private final String dateTime;
	private int oldFps;
	
	public ExBitmapFontActivity() {
		dateTime = new Date().toLocaleString();
	}

	@Override
	public void setupScenegraph() {
		cur = getRenderer();
		
		// create a new scene
		Scene myScene = new Scene();
		
		Mesh boxMesh = Box.createTexturedBox(cur);
		
		AndroidResourceResolver resolver = new AndroidResourceResolver(getAssets());
		
		Bitmap font = BitmapFactory.decodeStream(resolver.getResourceStream("nehe_texture_crate.png"));
		
		// create a new box and set a texture
		box = new MeshNode();
		box.setGeometry(boxMesh);
		boxMesh.setTexture(cur.getTextureManager().addImage(font, false));
		
		// add the box to the scene
		myScene.addChild(box);
		
		// set the scene to be rendered
		cur.setScene(myScene);
		
		Options opt = new  Options();
		opt.inPreferredConfig = Config.RGB_565;
		
		font = BitmapFactory.decodeStream(resolver.getResourceStream("font.png"));
		
		BitmapFont bmpFont = new BitmapFont(cur.getTextureManager().addImage(font, true));
		myLabel = new Label(cur);
		myLabel.setBitmapFont(bmpFont);
		
		
	
		
		
	}
	
	@Override
	public void updateScenegraph() {
		

		// rotate our box a little
		box.rotate(1, 0.25f, 1, 0);
		
		cur.setOrthographicProjection();
		if (cur.getFpsCounter().getLastFps() != oldFps) {
			
			oldFps = cur.getFpsCounter().getLastFps();
		}
		myLabel.setText(cur.getFpsCounter().toString());
		myLabel.setColor(Color.WHITE);
		myLabel.render(cur.getGl());
		
	}
	
	
}
